# To change this template, choose Tools | Templates
# and open the template in the editor.


from game.gui.widget import *

class Label(Widget):
    def __init__(self, **args):
        Widget.__init__(self, **args)

        self.text = ""

    #Will make the dimensions of the label just fit the size of the text.
    #The origin of this widget's rect will remain the same
    def pack(self):
        self.rect.size = self.constrain(self.font.size(self.text))
        return self.rect.size

    def set_text(self, text):
        self.text = text
        self.make_dirty()

    def draw_img(self):
        offset = [0, 0]
        w, h = self.font.size(self.text)

        #take into account the aligns
        #center horizontal align
        if self.align == 0:
            offset[0] = (self.rect.width - w) // 2

        #right align
        elif self.align == 1:
            offset[0] = self.rect.width - w

        if self.valign == 0:
            offset[1] = (self.rect.height - h) // 2
        elif self.valign == 1:
            offset[1] = self.rect.height - h

        text_img = self.font.render(self.text, True, self.color)
        self.image = pygame.Surface(self.rect[2], self.rect[3]).convert()

        self.image.blit(text_img, (offset[0], offset[1]))


#A multi label is a label that can span multiple lines, constrained by a max_width
class MultiLabel(Widget):
    def __init__(self, **args):
        Widget.__init__(self, **args)
        self.text_lines = []

    #text is sent to this with line breaks specified as a '<br>'.
    #This acts as an explicit line break, while the widget itself will handle implicit
    #line breaks (single lines too long for the max_width)
    def set_text(self, text):
        ex_lines = text.split('<br>')

        fin_lines = []
        mw = self.rect.width

        for im_line in ex_lines:
            s = self.font.size(im_line)
            if s[0] < mw:
                fin_lines.append(im_line)
            else:
                words = im_line.split(' ')
                c_line = []
                p_len = 0
                for word in words:
                    p_len = p_len + self.font.size(word)[0]
                    if p_len > mw:
                        fin_lines.append(' '.join(c_line))
                        c_line = [word]
                        p_len = self.font.size(word)[0]
                    else:
                        c_line.append(word)
                if len(c_line) > 0:
                    fin_lines.append(' '.join(c_line))

        self.text_lines = fin_lines
        self.make_dirty()

    def draw_img(self):
        offset = [0, 0]
        h = self.font.get_linesize() * len(self.text_lines)
        if self.valign == 0:
            offset[1] = (self.rect.height - h) // 2
        elif self.valign == 1:
            offset[1] = self.rect.height - h

        self.image = pygame.Surface((self.rect[2], self.rect[3])).convert()

        for line in self.text_lines:
            w, h = self.font.size(line)

            #take into account the aligns
            #center horizontal align
            if self.align == 0:
                offset[0] = (self.rect.width - w) // 2

            #right align
            elif self.align == 1:
                offset[0] = self.rect.width - w

            line_img = self.font.render(line, True, self.color)
            self.image.blit(line_img, (offset[0], offset[1]))
            offset[1] += self.font.get_linesize()